import { contextBridge, ipcRenderer } from "electron";
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)

    contextBridge.exposeInMainWorld('electronAPI', {
      ipInfo: (info) => ipcRenderer.on('ipInfo', info), // ip的相关信息

      reqAllTyre: () => ipcRenderer.invoke('reqAllTyre'), // 请求轮胎
      resAllTyre: (res) => ipcRenderer.on('resAllTyre', res), // 请求轮胎的响应

      // 生成结果文件
      createOutputFile: (content, fileName) => ipcRenderer.invoke('createOutputFile', content, fileName),
      createOutputFileRes: (flag) => ipcRenderer.on('createOutputFileRes', flag), // 命令行启动携带的参数

      commandArgv: (content) => ipcRenderer.on('commandArgv', content), // 命令行启动携带的参数
    });
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
